'use strict';

module.exports = app => {
  const { model } = app;
  const { UUID, STRING, BOOLEAN } = app.Sequelize;

  const Resource = model.define(
    'Resource',
    {
      id: {
        type: UUID,
        primaryKey: true,
      },
      name: {
        type: STRING(50),
        allowNull: false,
      },
      url: {
        type: STRING(255),
        allowNull: false,
      },
      description: {
        type: STRING(255),
        allowNull: false,
      },
      active: {
        type: BOOLEAN,
        defaultValue: true,
      },
    },
    {
      tableName: 'sys_resource',
    }
  );

  Resource.associate = function() {
    const { Sys: { Permission, Resource, PermissionResource } } = model;
    Resource.belongsToMany(Permission, { as: 'permissions', foreignKey: 'resource_id', through: PermissionResource });
  };

  return Resource;
};
